# -*- coding:utf-8 -*-
from heuristics.Pile_Division import *
from heuristics.Manhattan_Obst import *
from search.all import *
from multi_robot_problem import MultiRobotState
from time import *
from generate_output import *
from problem_generator import ProblemGenerator
"""
Created on Apr 11, 2011

@author: bob
"""

' algorithms initialization '
algo_gbf_any = BestFirstAnyTimeGraphSearch()
algo_gbf = BestFirstGraphSearch()
algo_astar_any = AStarAnytime()
algo_astar_any_2 = AStarAnytime2()
algo_astar = AStar()

num_of_problems = 2
time1 = 10
time2 = 30
time3 = 60
#filename_gbf = "C:\\Users\\Hagut\\Desktop\\AI\\output_gbf.txt"
#filename_astar = "C:\\Users\\Hagut\\Desktop\\AI\\output_astar.txt"
#filename_time = "C:\\Users\\Hagut\\Desktop\\AI\\output_time.txt"
filename_pile = "/home/smhagut/ai/output_pile_hard.txt"
filename_man = "/home/smhagut/ai/output_man_hard.txt"

'initializing random problems generator'
'''
minWidth, maxWidth = 10,10
minHeight, maxHeight = 10,10
maxRobots, maxPiles = 4, 7
maxObstacles, obstacleType = 0, Tester.OBSTACLES_ALL
wallsLenght, wallsVariance = 3, 2
pocketWidth, pocketWidthVariance = 3, 3
pocketHeight, pocketHeightVariance = 3, 3 

'''


height, width = 10,10
minRobs, maxRobs = 4,5
difficulty = ProblemGenerator.HARD
minPiles, maxPiles, minObs, maxObs = 6, 8, 3, 4 
minWallLength, maxWallLength = 5,7

pGen = ProblemGenerator(height, width, minRobs, maxRobs, difficulty,
    minPiles, maxPiles, minObs, maxObs, minWallLength, maxWallLength)

print "Running started."
hours = 2*(time1+time2+time3)*num_of_problems/3600
minutes = (2*(time1+time2+time3)*num_of_problems/60) %60
seconds = (2*(time1+time2+time3)*num_of_problems) % 60
print "expected running time:",hours,":",minutes,":",seconds


#file_gbf = open(filename_gbf,"a")
#file_astar = open(filename_astar,"a")
#file_time = open(filename_time,"a")
file_pile = open(filename_pile,"a")
file_man = open(filename_man,"a")

#file_gbf.write("FOUND?\tlength\tsols\tFirst solution time\n")
#file_astar.write("FOUND?\tlength\tsols\tFirst solution time\n")
#file_time.write("FOUND?\tlength\tsols\tFirst solution time\n")
file_pile.write("FOUND?\tlength\tsols\tFirst solution time\n")
file_man.write("FOUND?\tlength\tsols\tFirst solution time\n")

file_pile.close()
file_man.close()
'''
for x in xrange(0,num_of_problems):
    print "Computing problem", x+1, "out of", num_of_problems
    problem = pGen.trollProblem()
    generate_output(file_gbf, problem, algo_gbf_any, Pile_Division, time_for_problem)
    generate_output(file_astar, problem, algo_astar_any_2, Pile_Division, time_for_problem)
'''
start_time = clock()
for x in xrange(0,num_of_problems):
    print "Computing problem", x+1, "out of", num_of_problems
    problem = pGen.trollProblem()
    print problem
    file_pile = open(filename_pile,"a")
    file_man = open(filename_man,"a")
    'calculate for gbf'
    file_pile.write("problem number "+str(x+1)+" :\n")
    generate_output(file_pile, problem, algo_gbf_any, Pile_Division, time1)
    generate_output(file_pile, problem, algo_gbf_any, Pile_Division, time2)
    generate_output(file_pile, problem, algo_gbf_any, Pile_Division, time3)
    'calculate for astar'
    file_man.write("problem number "+str(x+1)+" :\n")
    generate_output(file_man, problem, algo_gbf_any, Manhattan_Obst, time1)
    generate_output(file_man, problem, algo_gbf_any, Manhattan_Obst, time2)
    generate_output(file_man, problem, algo_gbf_any, Manhattan_Obst, time3)
    'running time for each iteration: (time1+time2+time3)*2'
    file_pile.close()
    file_man.close()
print "~~ Running finished. "
#print "Running time =", clock()-start_time
'''
width = 10
height = 10
robots = ((4, 3), (0, 7), (2, 4), (3, 9), (1, 8))
dirt_locations = frozenset([(5, 7), (3, 4), (4, 6), (6, 0), (2, 2)])
obstacles = frozenset([(7, 1), (6, 1), (3, 1), (2, 1), (1, 1), (0, 6), (0, 5), (5, 1), (4, 1), (9, 8)])
problem = MultiRobotState(width, height, robots, dirt_locations, obstacles)

generate_output(file_pile, problem, algo_gbf_any, Pile_Division, time_for_problem)
generate_output(file_man, problem, algo_gbf_any, Manhattan_Obst, time_for_problem)
'''